package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.pojo.Permission;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.PermissionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PermissionController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/6 16:13
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @DubboReference
    private PermissionService permissionService;

    /**
     * 新增权限信息
     * @param permission
     * @return
     */
    @PostMapping("/add")
    public BaseResult<Permission> add(@RequestBody Permission permission){
        permissionService.addPermission(permission);
        return BaseResult.OK(permission);
    }

    /**
     * 删除权限信息
     * @param pid
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult<Long> delete(Long pid){
        permissionService.delPermission(pid);
        return BaseResult.OK(pid);
    }

    /**
     * 更新权限信息
     * @param permission
     * @return
     */
    @PutMapping("/update")
    public BaseResult<Permission> update(@RequestBody Permission permission){
        System.out.println("permission:"+permission);
        permissionService.updatePermission(permission);
        return BaseResult.OK(permission);
    }

    /**
     * 根据ID查询权限信息
     * @param pid
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Permission> findById(Long pid){
        Permission permission = permissionService.findById(pid);
        return BaseResult.OK(permission);
    }

    /**
     * 查询所有的权限信息
     * @param pid
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult<List<Permission>> findAll(Long pid){
        List<Permission> all = permissionService.findAll();
        return BaseResult.OK(all);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    public BaseResult<Page<Permission>> search(int page, int size){
        Page<Permission> page1 = permissionService.search(page, size);
        return BaseResult.OK(page1);
    }
}




